﻿/*************************** Copyright *****************************\
 * Copyright 2011 Sendine.com - http://www.sendine.com/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
\*******************************************************************/
using System;
using System.IO;
using System.Web.UI;
using System.Web;
using Sendine.Web.Route.Cfg;
using System.Text.RegularExpressions;

namespace Sendine.Web.Route
{
    public class HttpHandlerFactory : BaseModuleRedirector, IHttpHandlerFactory
    {
        protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
        {
            RouterRule[] rules = RouterConfiguration.GetConfig().CurrentDomain.Rules;
            foreach (var rule in rules)
            {
                RuleValidator validator = RuleValidator.Create(rule);
                if (rule.IsRegexValidator)
                {
                    if (validator.Match(requestedPath))
                    {
                        RouterUtility.RewriteUrl(app.Context, validator.Url, string.Empty);
                    }
                }
            }
        }

        public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            context.Trace.Write("HttpHandlerFactory", "Entering RewriterFactoryHandler");

            string sendToUrl = url;
            string filePath = pathTranslated;

            RouterRule[] rules = RouterConfiguration.GetConfig().CurrentDomain.Rules;

            foreach (var rule in rules)
            {
                RuleValidator validator = RuleValidator.Create(rule);
                if (!validator.Match(url))
                    continue;

                if (rule.IsRegexValidator)
                {
                    sendToUrl = validator.Url;
                    context.Trace.Write("HttpHandlerFactory", "Found match, rewriting to " + sendToUrl);
                    string sendToUrlLessQString;
                    RouterUtility.RewriteUrl(context, sendToUrl, string.Empty, out sendToUrlLessQString, out filePath);
                    context.Trace.Write("HttpHandlerFactory", "Exiting HttpHandlerFactory");
                    return PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context);
                }
                else
                {
                    IHttpHandlerFactory factory = validator.GetHandlerFactory();
                    context.Trace.Write("HttpHandlerFactory", "Found match, get factory " + factory.ToString());
                    context.Trace.Write("HttpHandlerFactory", "Exiting HttpHandlerFactory");
                    return factory.GetHandler(context, requestType, url, pathTranslated);
                }
            }

            context.Trace.Write("HttpHandlerFactory", "Exiting HttpHandlerFactory");
            return PageParser.GetCompiledPageInstance(url, filePath, context);
        }

        public virtual void ReleaseHandler(IHttpHandler handler)
        {

        }
    }
}
